package org.jsbd.boss.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.domian.BaseInfo;
import org.jsbd.boss.domian.statistics.MiguUserMonitorVO;
import org.jsbd.boss.domian.statistics.MiguUserStatisticsDTO;
import org.jsbd.boss.service.IBaseInfoService;
import org.jsbd.boss.service.IMiguUserMonitorService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/migu")
public class MiguUserMonitorController {
	public static final Logger LOGGER = LoggerFactory.getLogger(MiguUserMonitorController.class);
	
	@Resource
	private IMiguUserMonitorService miguUserMonitorService;
	@Resource
	private IBaseInfoService baseInfoService;

	@RequestMapping(value = "/monitorPage", method = RequestMethod.GET)
	public String monitorPage(Model model) {
		return "/monitor/miguUserMonitor";
	}

	@RequestMapping(value = "/miguUserMonitor", method = RequestMethod.POST)
	@ResponseBody
	public Map list(Model model, MiguUserMonitorVO mumVO) {
		Map<String, Object> map = miguUserMonitorService.findMiguUserStatisticsInfo(mumVO);
		return map;
	}
	
	@RequestMapping(value = "/monitorDayPage", method = RequestMethod.GET)
	public String monitorDayPage(Model model) {
		return "/monitor/miguUserDayMonitor";
	}
	
	@RequestMapping(value = "/miguUserDayMonitor", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> miguUserDayMonitor(Model model, MiguUserMonitorVO mumVO) {
//		List list = miguUserMonitorService.findMiguUserDayStatisticsInfo(mumVO);
//		return list;
		String startTime = mumVO.getStartTime();
		String endTime = mumVO.getEndTime();
		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
			mumVO.setStartTime(DateUtil.getHourBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD_HHMMSS, 2));//默认查询2小时内数据
		}
		Map<String, Object> map =  miguUserMonitorService.findMiguUserDayStatisticsInfo(mumVO);
		return map;
	}
	
	@RequestMapping(value = "/monitorPageNew", method = RequestMethod.GET)
	public String monitorPageNew(Model model) {
		//当天剩余量
  		List<MiguUserStatisticsDTO> list_residue = miguUserMonitorService.selectResidue(DateUtil.getY_M_DStr());
  		for (MiguUserStatisticsDTO dto : list_residue) {
  			BaseInfo bi = baseInfoService.getBaseInfoById(dto.getBaseId());
  			dto.setBaseName(bi.getBaseName());
  		}
  		model.addAttribute("residues", list_residue);
		return "/monitor/miguUserMonitorNew";
	}
	
	@RequestMapping(value = "/miguUserMonitorNew", method = RequestMethod.POST)
	@ResponseBody
	public Map miguUserMonitorNew(Model model, MiguUserMonitorVO mumVO) {
		String startTime = mumVO.getStartTime();
  		String endTime = mumVO.getEndTime();
  		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
  			mumVO.setStartTime(DateUtil.getDayBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD, 30));
  		}
  		
  		//图表数据
		List<String> xAxis =  miguUserMonitorService.selectDays(mumVO);
  		List<MiguUserStatisticsDTO> yAxis =  miguUserMonitorService.selectRecords(mumVO);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);
		return map;
	}

}
